﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Diagnostics;
using System.Windows.Controls;
using System.Collections.ObjectModel;
using System.Windows;
using System.Threading;

namespace InscriberControlClient
{
    public class Titrai
    {
        public int nr { get; set; }
        public string Title { get; set; }
        public string StartTime { get; set; }
        public string StopTime { get; set; }
    }

    public class Lydai
    {
        public int nr { get; set; }
        public string Title { get; set; }
        public string Count { get; set; }
        public string Type { get; set; }
        public bool TypeK { get; set; }
    }

    public class Vedejai
    {
        public int nr { get; set; }
        public string Title { get; set; }
    }

    public class Logo
    {
        public int nr { get; set; }
        public string Title { get; set; }
        public string StartTime { get; set; }
        public string StopTime { get; set; }
    }

    public class Anonsai
    {
        public int nr { get; set; }
        public string Title { get; set; }
    }

    public class AnonsaiParts
    {
        public int id { get; set; }
        public int anonso_id { get; set; }
        public string tekstas { get; set; }
        public bool eteris { get; set; }
    }

    public class AnonsaiPartsEtersis
    {
        public int id { get; set; }
        public string tekstas { get; set; }

    }

    class Functionality
    {
        static private string dbServer = "localhost\\SQLExpress";
        static private string db = "DB";
        static private string db_user = "";
        static private string db_password = "";
        static private string db_config_tbl = "TB";
        static public string LDid = "";
        static public selitem si = new selitem();
        public static Window1 mainW = null;
        public static string type_tmp = "";
        static public bool karstaNaujiena = false;
        static public bool autoTimer = false;
        static public string currentLeadId = "0";
        static public bool stopped = true;
        static public string tmpreport_id = "";
        static public System.Windows.Point lastPosition = new Point();
        static public bool startedLD = false;


        public struct selitem
        {
            public string news_id;
            public string rep_id;
            public string repname;
        }

        public static string StartPath
        {
            get
            {
                return System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase).Substring(6);
            }
        }

        public static string ToTime(DateTime tm)
        {
            //tm.To
            string time = "00:00:00";
            string hours = tm.Hour.ToString();
            if (hours.Length == 1)
            {
                hours = "0" + hours;
            }
            string minutes = tm.Minute.ToString();
            if (minutes.Length == 1)
            {
                minutes = "0" + minutes;
            }
            string seconds = tm.Second.ToString();
            if (seconds.Length == 1)
            {
                seconds = "0" + seconds;
            }
            time = hours + ":" + minutes + ":" + seconds;
            return time;
        }

        public static string ToDate(DateTime tm)
        {
            string time = "00:00:00";
            string Years = tm.Year.ToString();
            string Months = tm.Month.ToString();
            if (Months.Length == 1)
            {
                Months = "0" + Months;
            }
            string Days = tm.Day.ToString();
            if (Days.Length == 1)
            {
                Days = "0" + Days;
            }
            time = Years + "-" + Months + "-" + Days;
            return time;
        }
        //get current date
        static public string getDate()
        {
            return ToDate(DateTime.Now);
        }

        //get current Time
        static public string getTime()
        {
            return ToTime(DateTime.Now);
        }

        public static void readXMLParameters()
        {
            try
            {
                XmlTextReader textReader = new XmlTextReader(StartPath + "\\config.xml");

                while (textReader.Read())
                {
                    XmlNodeType nType = textReader.NodeType;
                    if (nType == XmlNodeType.Element)
                    {
                        if (textReader.Name.ToString().Equals("DataBaseServer"))
                        {
                            dbServer = textReader.ReadElementContentAsString();
                        }
                        if (textReader.Name.ToString().Equals("DataBaseName"))
                        {
                            db = textReader.ReadElementContentAsString();
                        }
                        if (textReader.Name.ToString().Equals("DataBaseUser"))
                        {
                            db_user = textReader.ReadElementContentAsString();
                        }
                        if (textReader.Name.ToString().Equals("DataBasePassword"))
                        {
                            db_password = textReader.ReadElementContentAsString();
                        }
                        if (textReader.Name.ToString().Equals("ConfigTable"))
                        {
                            db_config_tbl = textReader.ReadElementContentAsString();
                        }
                       
                    }
                }
            }
            catch (Exception ex) { AddToEventLog("InscriberControlClient", "XML skaitymo klaida: " + ex.Message, true); }
        }
         public static void AddToEventLog(string sSource, string sEvent, bool error)
        {
            string sLog = "Application";

            if (!EventLog.SourceExists(sSource))
                EventLog.CreateEventSource(sSource, sLog);
            if (error)
            {
                EventLog.WriteEntry(sSource, sEvent, EventLogEntryType.Error, 0);
            }
            else
            {
                EventLog.WriteEntry(sSource, sEvent, EventLogEntryType.Information, 123);
            }

        }

         public static void startRL(CheckBox ch)
         {
             DataBase dbc = new DataBase(dbServer, db_user, db_password, db);
             dbc.sqlExecute("update StartConf set RL_start='True', VT_start='"+ch.IsChecked.ToString()+"' where id=1");
             dbc = null;
         }

         public static void stopRL()
         {
             DataBase dbc = new DataBase(dbServer, db_user, db_password, db);
             dbc.sqlExecute("update StartConf set RL_start='False' where id=1");
             dbc = null;
         }

         public static bool getStatusRL(Button bt,CheckBox ch)
         {
             bool rez=false;
             DataBase dbc = new DataBase(dbServer, db_user, db_password, db);
             System.Data.DataTable tb=dbc.getTable("select * from StartConf where id=1");
             ch.IsChecked = bool.Parse(tb.Rows[0]["VT_start"].ToString());
             if (tb.Rows[0]["RL_start"].ToString().Equals("True")){
                 rez=true;
                 bt.Content = "Stabdyti eilutę";
             }else{
                 rez=false;
                 bt.Content = "Paleisti eilutę";
             }
             dbc = null;
             return rez;
         }

         public static bool getStatusLD(Window wd,Button bt)
         {
             bool rez = false;
             DataBase dbc = new DataBase(dbServer, db_user, db_password, db);
             System.Data.DataTable tb = dbc.getTable("select * from StartConf where id=1");
             if (tb.Rows[0]["LD_start"].ToString().Equals("True"))
             {
                 rez = true;
                 bt.Style =(System.Windows.Style)wd.FindResource("InformButtonRED");
             }
             else
             {
                 rez = false;
                 bt.Style = (System.Windows.Style)wd.FindResource("InformButton");
             }
             dbc = null;
             return rez;
         }

         public static bool getStatusAuto(Window wd, Button bt1, Button bt2, Button bt3)
         {
             bool rez = false;
             DataBase dbc = new DataBase(dbServer, db_user, db_password, db);
             System.Data.DataTable tb = dbc.getTable("select * from StartConf where id=1");
                       if (tb.Rows[0]["TimerAuto"].ToString().Equals("True"))
             {
                 rez = true;
                 autoTimer = true;
                 bt1.Style = (System.Windows.Style)wd.FindResource("InformButtonGREEN");
                 bt2.Style = (System.Windows.Style)wd.FindResource("InformButtonGRAY");
                // bt3.Visibility = Visibility.Visible;
             }
             else
             {
                 rez = false;
                 autoTimer = false;
                 bt1.Style = (System.Windows.Style)wd.FindResource("InformButtonGRAY");
                 bt2.Style = (System.Windows.Style)wd.FindResource("InformButtonGREEN");
                // bt3.Visibility = Visibility.Hidden;
             }
             dbc = null;
             return rez;
         }

         public static void updateAuto(int i)
         {
             DataBase dbc = new DataBase(dbServer, db_user, db_password, db);
             if (i==0)
             {
                 dbc.sqlExecute("update StartConf set TimerAuto='False' where id=1");
             }
             else
             {
                 dbc.sqlExecute("update StartConf set TimerAuto='True' where id=1");
             }
         }

         public static ObservableCollection<Titrai> getTitrai(string report_id)
         {
             ObservableCollection<Titrai> tr = new ObservableCollection<Titrai>();
             DataBase dbc = new DataBase(dbServer, db_user, db_password, db);
             string sql = "select * from titrai";
             if (!report_id.Equals(""))
             {
                 sql = "select * from titrai where report_id=" + report_id + " order by id asc";
             }
             System.Data.DataTable tbt = dbc.getTable(sql);
             for (int i = 0; i < tbt.Rows.Count; i++)
             {
                 try
                 {
                     int id = int.Parse(tbt.Rows[i]["id"].ToString());
                     string asmuo = tbt.Rows[i]["asmuo"].ToString();
                     string pareigos = tbt.Rows[i]["pareigos"].ToString();
                     string starttime = tbt.Rows[i]["start_time"].ToString().Substring(3, 5);
                     string stoptime = tbt.Rows[i]["end_time"].ToString().Substring(3, 5);
                     tr.Add(new Titrai() { nr = id, Title = asmuo, StartTime = starttime, StopTime = stoptime });
                 }
                 catch (Exception ex) { }

                // string komanda="W\\T"+id+"\\titras\\"+asmuo+"\\"+pareigos+"\\\\";
                // dbc.sqlExecute("insert into komandos (komanda,laikas,data) values ('"+komanda+"','','')"); 
             }
             return tr;
         }

         public static void generateTitraiTemplates(string report_id)
         {
             DataBase dbc = new DataBase(dbServer, db_user, db_password, db);
             string sql = "select * from titrai";
             if (!report_id.Equals(""))
             {
                 sql = "select * from titrai where report_id=" + report_id + " order by id asc";
             }
             System.Data.DataTable tbt = dbc.getTable(sql);
             for (int i = 0; i < tbt.Rows.Count; i++)
             {
                 int id = int.Parse(tbt.Rows[i]["id"].ToString());
                 string asmuo = tbt.Rows[i]["asmuo"].ToString().Replace("'", "''");
                 string pareigos = tbt.Rows[i]["pareigos"].ToString().Replace("'", "''");
                 string starttime = tbt.Rows[i]["start_time"].ToString().Substring(3, 5);
                 string stoptime = tbt.Rows[i]["end_time"].ToString().Substring(3, 5);
                 string komanda = "W\\\\T" + id + "\\\\titras\\\\" + asmuo + "\\\\" + pareigos + "\\\\\\\\";
                 dbc.sqlExecute("insert into komandos (komanda,laikas,data) values ('" + komanda + "','','')");
             }
         }

         public static ObservableCollection<Lydai> getLydai(string report_id)
         {
             if ((!report_id.Equals("")) && (!report_id.Equals("X"))){
                 tmpreport_id=report_id;
             }
             if (report_id.Equals("X"))
             {
                 report_id = tmpreport_id;
             }
             ObservableCollection<Lydai> ld = new ObservableCollection<Lydai>();
             DataBase dbc = new DataBase(dbServer, db_user, db_password, db);
             string sql = "select * from leads";
             if (!report_id.Equals(""))
             {
                 sql = "select * from leads where report_id=" + report_id + " order by id asc";
             }
             System.Data.DataTable tbt = dbc.getTable(sql);
             for (int i = 0; i < tbt.Rows.Count; i++)
             {
                 int id = int.Parse(tbt.Rows[i]["id"].ToString());
                 string pavadinimas = tbt.Rows[i]["title"].ToString();
                 string tipas= tbt.Rows[i]["template"].ToString();
                 System.Data.DataTable tbc = dbc.getTable("select count(*) from leads_part where lead_id="+id.ToString());
                 string cnt = tbc.Rows[0][0].ToString();
                 bool tipasK = false;
                 if (tipas.Equals("K"))
                 {
                     tipasK = true;
                 }
                 else { tipasK = false; }
                 ld.Add(new Lydai() { nr = id, Title = pavadinimas, Type = tipas,TypeK=tipasK, Count=cnt });
             /*   string komanda = "W\\L" + id + "\\lydas\\" + pavadinimas  + "\\\\";
                dbc.sqlExecute("insert into komandos (komanda,laikas,data) values ('" + komanda + "','','')");
                 System.Data.DataTable tbtp = dbc.getTable("select * from leads_part where lead_id="+id);
                 int idp = 0;
                 for (int y = 0; y < tbtp.Rows.Count; y++)
                 {
                     idp++;
                     //int idp = int.Parse(tbtp.Rows[y]["id"].ToString());
                     string tekstas1 = tbtp.Rows[y]["tekstas1"].ToString();
                     string tekstas2 = tbtp.Rows[y]["tekstas2"].ToString();
                     string template = tbtp.Rows[y]["template"].ToString();
                     komanda = "W\\LP" + id + "_" + idp + "\\" + template + "\\" + tekstas1 + "\\"+tekstas2+"\\\\";
                     dbc.sqlExecute("insert into komandos (komanda,laikas,data) values ('" + komanda + "','','')");
                 }
                 sql = "update leads set parts_cnt=" + tbtp.Rows.Count + " where id=" + id;
                 dbc.sqlExecute(sql); */
             }
             return ld;
         }

         public static void updateLeadTemplateToKById(string id)
         {
             DataBase dbc = new DataBase(dbServer, db_user, db_password, db);
             string sql = "update leads set template='K' where id="+id;
             dbc.sqlExecute(sql);
         }

         public static void updateLeadTemplateToNById(string id)
         {
             DataBase dbc = new DataBase(dbServer, db_user, db_password, db);
             string sql = "update leads set template='N' where id=" + id;
             dbc.sqlExecute(sql);
         }

         public static void generateLydaiTemplates(string report_id)
         {
             DataBase dbc = new DataBase(dbServer, db_user, db_password, db);
             string sql = "select * from leads";
             if (!report_id.Equals(""))
             {
                 sql = "select * from leads where report_id=" + report_id + " order by id asc";
             }
             System.Data.DataTable tbt = dbc.getTable(sql);
             for (int i = 0; i < tbt.Rows.Count; i++)
             {
                 int id = int.Parse(tbt.Rows[i]["id"].ToString());
                 string pavadinimas = tbt.Rows[i]["title"].ToString();
                 string tipas = tbt.Rows[i]["template"].ToString();
                 string komanda = "W\\\\L" + id + "\\\\lydas\\\\" + pavadinimas.Replace("'", "''") + "\\\\\\\\";
                 dbc.sqlExecute("insert into komandos (komanda,laikas,data) values ('" + komanda + "','','')");
                 System.Data.DataTable tbtp = dbc.getTable("select * from leads_part where lead_id=" + id+ " order by id asc");
                 int idp = 0;
                 for (int y = 0; y < tbtp.Rows.Count; y++)
                 {
                     idp++;
                     //int idp = int.Parse(tbtp.Rows[y]["id"].ToString());
                     string tekstas1 = tbtp.Rows[y]["tekstas1"].ToString().Replace("'", "''");
                     string tekstas2 = tbtp.Rows[y]["tekstas2"].ToString().Replace("'", "''");
                     string template = tbtp.Rows[y]["template"].ToString();
                     komanda = "W\\\\LP" + id + "_" + idp + "\\\\" + template + "\\\\" + tekstas1 + "\\\\" + tekstas2 + "\\\\\\\\";
                     dbc.sqlExecute("insert into komandos (komanda,laikas,data) values ('" + komanda + "','','')");
                 }
                 sql = "update leads set parts_cnt=" + tbtp.Rows.Count + " where id=" + id;
                 dbc.sqlExecute(sql);
             }
         }

         public static ObservableCollection<Logo> getLogo(string report_id)
         {
             ObservableCollection<Logo> lg = new ObservableCollection<Logo>();
             DataBase dbc = new DataBase(dbServer, db_user, db_password, db);
             string sql = "select * from logos";
             if (!report_id.Equals(""))
             {
                 sql = "select * from logos where report_id=" + report_id + " order by id asc";
             }
             System.Data.DataTable tbt = dbc.getTable(sql);
             for (int i = 0; i < tbt.Rows.Count; i++)
             {
                 int id = int.Parse(tbt.Rows[i]["id"].ToString());
                 string name = tbt.Rows[i]["name"].ToString();
                 string template = tbt.Rows[i]["template"].ToString();
                 string tekstas = tbt.Rows[i]["tekstas"].ToString();
                 string startt= tbt.Rows[i]["start_time"].ToString().Substring(3, 5);
                 string stopt = tbt.Rows[i]["end_time"].ToString().Substring(3, 5);
                 string reverse = tbt.Rows[i]["reverse"].ToString();
                 lg.Add(new Logo() { nr = id, Title = name , StartTime = startt, StopTime= stopt});
               /*  string komanda ="";
                 if (tekstas.Equals(""))
                 {
                     komanda = "W\\LG" + id + "\\" + template + "\\\\";
                     dbc.sqlExecute("insert into komandos (komanda,laikas,data) values ('" + komanda + "','','')");
                     if (reverse.Equals("True"))
                     {
                         komanda = "W\\LG" + id + "out\\" + template + "_out\\\\";
                         dbc.sqlExecute("insert into komandos (komanda,laikas,data) values ('" + komanda + "','','')");
                     }
                 }
                 else
                 {
                     komanda = "W\\LG" + id + "\\" + template + "\\"+tekstas+"\\\\";
                     dbc.sqlExecute("insert into komandos (komanda,laikas,data) values ('" + komanda + "','','')");
                     if (reverse.Equals("True"))
                     {
                         komanda = "W\\LG" + id + "out\\" + template + "_out\\" + tekstas + "\\\\";
                         dbc.sqlExecute("insert into komandos (komanda,laikas,data) values ('" + komanda + "','','')");
                     }
                 }*/
                 
             }
             return lg;
         }

         public static void generateLogoTemplates(string report_id)
         {
             ObservableCollection<Logo> lg = new ObservableCollection<Logo>();
             DataBase dbc = new DataBase(dbServer, db_user, db_password, db);
             string sql = "select * from logos";
             if (!report_id.Equals(""))
             {
                 sql = "select * from logos where report_id=" + report_id + " order by id asc";
             }
             System.Data.DataTable tbt = dbc.getTable(sql);
             for (int i = 0; i < tbt.Rows.Count; i++)
             {
                 int id = int.Parse(tbt.Rows[i]["id"].ToString());
                 string name = tbt.Rows[i]["name"].ToString();
                 string template = tbt.Rows[i]["template"].ToString();
                 string tekstas = tbt.Rows[i]["tekstas"].ToString().Replace("'", "''");
                 string startt = tbt.Rows[i]["start_time"].ToString().Substring(3, 5);
                 string stopt = tbt.Rows[i]["end_time"].ToString().Substring(3, 5);
                 string reverse = tbt.Rows[i]["reverse"].ToString();
                 string komanda = "";
                 if (tekstas.Equals(""))
                 {
                     komanda = "W\\\\LG" + id + "\\\\" + template + "\\\\\\\\";
                     dbc.sqlExecute("insert into komandos (komanda,laikas,data) values ('" + komanda + "','','')");
                     if (reverse.Equals("True"))
                     {
                         komanda = "W\\\\LG" + id + "out\\\\" + template + "_out\\\\\\\\";
                         dbc.sqlExecute("insert into komandos (komanda,laikas,data) values ('" + komanda + "','','')");
                     }
                 }
                 else
                 {
                     komanda = "W\\\\LG" + id + "\\\\" + template + "\\\\" + tekstas + "\\\\\\\\";
                     dbc.sqlExecute("insert into komandos (komanda,laikas,data) values ('" + komanda + "','','')");
                     if (reverse.Equals("True"))
                     {
                         komanda = "W\\\\LG" + id + "out\\\\" + template + "_out\\\\" + tekstas + "\\\\\\\\";
                         dbc.sqlExecute("insert into komandos (komanda,laikas,data) values ('" + komanda + "','','')");
                     }
                 }

             }
         }

         public static void startTR(string id)
         {
             if (!autoTimer)
             {
                 DataBase dbc = new DataBase(dbServer, db_user, db_password, db);
                 dbc.sqlExecute("update StartConf set TimerBreak='True' where id=1");
                 string komanda = "";
                 /* if (karstaNaujiena)
                  {
                      komanda = "T\\14\\B\\\\";
                      dbc.sqlExecute("insert into komandos (komanda,laikas,data) values ('" + komanda + "','','')");
                      komanda = "T\\7\\juosta_paprasta\\C\\\\";
                      dbc.sqlExecute("insert into komandos (komanda,laikas,data) values ('" + komanda + "','','')");
                  }*/
                 komanda = "I\\\\1\\\\2\\\\D\\\\A\\\\8\\\\0\\\\0\\\\1\\\\\\\\";
                 dbc.sqlExecute("insert into komandos (komanda,laikas,data) values ('" + komanda + "','','')");
                 komanda = "T\\\\7\\\\T" + id + "\\\\B\\\\\\\\";
                 dbc.sqlExecute("insert into komandos (komanda,laikas,data) values ('" + komanda + "','','')");
             }
             else
             {
                 MessageBox.Show("Tik rankiniame režime!");
             }
         }

         public static void stopTR()
         {
             if (!autoTimer)
             {
                 DataBase dbc = new DataBase(dbServer, db_user, db_password, db);
                 dbc.sqlExecute("update StartConf set TimerBreak='False' where id=1");
                 string komanda = "I\\\\1\\\\2\\\\D\\\\A\\\\8\\\\0\\\\0\\\\1\\\\\\\\";
                 dbc.sqlExecute("insert into komandos (komanda,laikas,data) values ('" + komanda + "','','')");
                 if (!startedLD){
                    komanda = "T\\\\7\\\\L" + currentLeadId+ "\\\\B\\\\\\\\";
                    dbc.sqlExecute("insert into komandos (komanda,laikas,data) values ('" + komanda + "','','')"); 
                 }
             }
             else
             {
                 MessageBox.Show("Tik rankiniame režime!");
             }
           /*  if (karstaNaujiena)
             {
                 komanda = "T\\14\\B\\\\";
                 dbc.sqlExecute("insert into komandos (komanda,laikas,data) values ('" + komanda + "','','')");
                 komanda = "T\\7\\juosta_paprasta_K\\C\\\\";
                 dbc.sqlExecute("insert into komandos (komanda,laikas,data) values ('" + komanda + "','','')");
             }*/
             
             
             //string komanda = "T\\14\\B\\\\";
             //dbc.sqlExecute("insert into komandos (komanda,laikas,data) values ('" + komanda + "','','')");
            
             //komanda = "T\\14\\C\\\\";
             //dbc.sqlExecute("insert into komandos (komanda,laikas,data) values ('" + komanda + "','','')");

         }

         public static void stopLD(String type)
         {
             if (!autoTimer)
             {
                 stopped = true;
                 mainW.ResetTimer();
                 //timer = false;
                 DataBase dbc = new DataBase(dbServer, db_user, db_password, db);
                 string komanda = "I\\\\1\\\\1\\\\D\\\\A\\\\8\\\\0\\\\0\\\\1\\\\\\\\";
                 dbc.sqlExecute("insert into komandos (komanda,laikas,data) values ('" + komanda + "','','')");
                 komanda = "I\\\\1\\\\2\\\\D\\\\A\\\\8\\\\0\\\\0\\\\1\\\\\\\\";
                 dbc.sqlExecute("insert into komandos (komanda,laikas,data) values ('" + komanda + "','','')");
                 komanda = "I\\\\1\\\\3\\\\D\\\\A\\\\8\\\\0\\\\0\\\\1\\\\\\\\";
                 dbc.sqlExecute("insert into komandos (komanda,laikas,data) values ('" + komanda + "','','')");

                 if (type.Equals("K"))
                 {
                     komanda = "T\\\\14\\\\A\\\\\\\\";
                     komanda = "T\\\\7\\\\karsta_naujiena_out\\\\A\\\\\\\\";
                     dbc.sqlExecute("insert into komandos (komanda,laikas,data) values ('" + komanda + "','','')");

                 }

                 komanda = "T\\\\14\\\\B\\\\\\\\";
                 komanda = "T\\\\7\\\\tuscias\\\\B\\\\\\\\";
                 dbc.sqlExecute("insert into komandos (komanda,laikas,data) values ('" + komanda + "','','')");
                 Thread.Sleep(200);
                 komanda = "T\\\\14\\\\C\\\\\\\\";
                 komanda = "T\\\\7\\\\tuscias\\\\C\\\\\\\\";
                 dbc.sqlExecute("insert into komandos (komanda,laikas,data) values ('" + komanda + "','','')");
                // dbc.sqlExecute("update StartConf set LD_start='False', Timer='False' where id=1");
             }
             else
             {
                 MessageBox.Show("Tik rankiniame režime!");
             }
         }

   
         public static void resetLD()
         {
             
             DataBase dbc = new DataBase(dbServer, db_user, db_password, db);
             string sql = "update StartConf set LD_start='False', Timer='False', TimerBreak='False' where id=1";
             dbc.sqlExecute(sql);
         }

         public static void startLDintro(string id,string type)
         {
 
                 currentLeadId = id;
                 if (type.Equals("K"))
                 {
                     karstaNaujiena = true;
                 }
                 else
                 {
                     karstaNaujiena = false;
                 }
                 LDid = id;
                 DataBase dbc = new DataBase(dbServer, db_user, db_password, db);

                 string komanda = "";

                 mainW.ResetTimer();
                 
                 if (!stopped)
                 {
                     

                     System.Data.DataTable tb = dbc.getTable("select * from leads where id=" + id);
                     string parts_cnt = tb.Rows[0]["parts_cnt"].ToString();
                     string sql = "update StartConf set LD_start='False', LD_id=" + id + ", LD_current=1, LD_cnt=" + parts_cnt + ", Timer='False' where id=1";
                     dbc.sqlExecute(sql);
                     if (!type_tmp.Equals(type))
                     {
                         komanda = "I\\\\1\\\\2\\\\D\\\\A\\\\8\\\\0\\\\0\\\\1\\\\\\\\";
                         dbc.sqlExecute("insert into komandos (komanda,laikas,data) values ('" + komanda + "','','')");
                         komanda = "T\\\\14\\\\B\\\\\\\\";
                         komanda = "T\\\\7\\\\tuscias\\\\B\\\\\\\\";
                         dbc.sqlExecute("insert into komandos (komanda,laikas,data) values ('" + komanda + "','','')");

                         komanda = "I\\\\1\\\\3\\\\D\\\\A\\\\8\\\\0\\\\0\\\\1\\\\\\\\";
                         dbc.sqlExecute("insert into komandos (komanda,laikas,data) values ('" + komanda + "','','')");



                         if (type_tmp.Equals("N") && type.Equals("K"))
                         {
                             komanda = "T\\\\7\\\\tuscias\\\\B\\\\\\\\";
                             dbc.sqlExecute("insert into komandos (komanda,laikas,data) values ('" + komanda + "','','')");
                             komanda = "T\\\\7\\\\tuscias\\\\C\\\\\\\\";
                             dbc.sqlExecute("insert into komandos (komanda,laikas,data) values ('" + komanda + "','','')");
                             Thread.Sleep(540);
                             komanda = "T\\\\7\\\\karsta_naujiena_in\\\\A\\\\\\\\";
                             dbc.sqlExecute("insert into komandos (komanda,laikas,data) values ('" + komanda + "','','')");
                             Thread.Sleep(4000);
                             komanda = "T\\\\7\\\\intro_juosta_K\\\\C\\\\\\\\";
                         }
                         else if (type_tmp.Equals("K") && type.Equals("N"))
                         {
                             komanda = "T\\\\7\\\\karsta_naujiena_out\\\\A\\\\\\\\";
                             dbc.sqlExecute("insert into komandos (komanda,laikas,data) values ('" + komanda + "','','')");
                             Thread.Sleep(750);
                             komanda = "T\\\\7\\\\juosta_paprasta\\\\C\\\\\\\\";
                         }

                         
                         dbc.sqlExecute("insert into komandos (komanda,laikas,data) values ('" + komanda + "','','')");
                         Thread.Sleep(200);

                         if (type.Equals("K"))
                         {
                             Thread.Sleep(500);
                             komanda = "T\\\\7\\\\karsta_naujiena_loop\\\\A\\\\\\\\";
                             dbc.sqlExecute("insert into komandos (komanda,laikas,data) values ('" + komanda + "','','')"); 
                         }
                     }
                     //komanda = "I\\\\1\\\\2\\\\D\\\\A\\\\8\\\\0\\\\0\\\\1\\\\\\\\";
                     //dbc.sqlExecute("insert into komandos (komanda,laikas,data) values ('" + komanda + "','','')");
                     komanda = "T\\\\7\\\\L" + id + "\\\\B\\\\\\\\";
                     dbc.sqlExecute("insert into komandos (komanda,laikas,data) values ('" + komanda + "','','')");
                     Thread.Sleep(500);

                     if (type.Equals("K"))
                     {
                         komanda = "T\\\\7\\\\juosta_paprasta_K\\\\C\\\\\\\\";
                     }
                     else
                     {
                         komanda = "T\\\\7\\\\juosta_paprasta\\\\C\\\\\\\\";
                     }
                     dbc.sqlExecute("insert into komandos (komanda,laikas,data) values ('" + komanda + "','','')");
                 }
                 else
                 {
                     komanda = "I\\\\1\\\\3\\\\D\\\\A\\\\8\\\\0\\\\0\\\\1\\\\\\\\";
                     dbc.sqlExecute("insert into komandos (komanda,laikas,data) values ('" + komanda + "','','')");
                 
                     if (type.Equals("K"))
                     {
                         komanda = "T\\\\7\\\\karsta_naujiena_in\\\\A\\\\\\\\";
                         dbc.sqlExecute("insert into komandos (komanda,laikas,data) values ('" + komanda + "','','')");
                         Thread.Sleep(3500);
                         komanda = "T\\\\7\\\\intro_juosta_K\\\\C\\\\\\\\";
                     }
                     else
                     {
                         komanda = "T\\\\7\\\\intro_juosta\\\\C\\\\\\\\";
                     }
                     dbc.sqlExecute("insert into komandos (komanda,laikas,data) values ('" + komanda + "','','')");
                     if (type.Equals("K"))
                     {
                         //Thread.Sleep(2500);
                         Thread.Sleep(500);
                         komanda = "T\\\\7\\\\karsta_naujiena_loop\\\\A\\\\\\\\";
                         dbc.sqlExecute("insert into komandos (komanda,laikas,data) values ('" + komanda + "','','')");
                     }
                     Thread.Sleep(250);

                     //komanda = "I\\\\1\\\\2\\\\D\\\\A\\\\8\\\\0\\\\0\\\\1\\\\\\\\";
                     //dbc.sqlExecute("insert into komandos (komanda,laikas,data) values ('" + komanda + "','','')");
                 
                     komanda = "T\\\\7\\\\L" + id + "\\\\B\\\\\\\\";
                     dbc.sqlExecute("insert into komandos (komanda,laikas,data) values ('" + komanda + "','','')");

                     Thread.Sleep(500);

                     if (type.Equals("K"))
                     {
                         komanda = "T\\\\7\\\\juosta_paprasta_K\\\\C\\\\\\\\";
                     }
                     else
                     {
                         komanda = "T\\\\7\\\\juosta_paprasta\\\\C\\\\\\\\";
                     }
                     dbc.sqlExecute("insert into komandos (komanda,laikas,data) values ('" + komanda + "','','')");
                     
                     Functionality.stopped = false;
                 }
                 type_tmp = type;
               
         }

         public static void startLDloop(string id,bool startTimer)
         {
             if (!id.Equals(""))
             {
                 autoTimer = startTimer;
                 DataBase dbc = new DataBase(dbServer, db_user, db_password, db);
                 // string komanda = "T\\7\\LP" + id + "\\C\\\\";
                 System.Data.DataTable tb = dbc.getTable("select * from leads where id=" + id);
                 string parts_cnt = tb.Rows[0]["parts_cnt"].ToString();
                 string type = tb.Rows[0]["template"].ToString();
                 string komanda = "";
                 komanda = "I\\\\1\\\\3\\\\D\\\\A\\\\8\\\\0\\\\0\\\\1\\\\\\\\";
                 dbc.sqlExecute("insert into komandos (komanda,laikas,data) values ('" + komanda + "','','')");
                 
                 if (type.Equals("K"))
                 {
                     komanda = "T\\\\7\\\\juosta_paprasta_K\\\\C\\\\\\\\";
                 }
                 else
                 {
                     komanda = "T\\\\7\\\\juosta_paprasta\\\\C\\\\\\\\";
                 }
                 dbc.sqlExecute("insert into komandos (komanda,laikas,data) values ('" + komanda + "','','')");
                 string sql = "update StartConf set LD_start='True', LD_id=" + id + ", LD_current=1, LD_cnt=" + parts_cnt + ", Timer='" + autoTimer.ToString() + "' where id=1";
                 dbc.sqlExecute(sql);
             }
         }

         public static void startLG(string id)
         {
             if (!autoTimer)
             {
                 DataBase dbc = new DataBase(dbServer, db_user, db_password, db);
                 string komanda = "I\\\\1\\\\1\\\\D\\\\A\\\\8\\\\0\\\\0\\\\1\\\\\\\\";
                 dbc.sqlExecute("insert into komandos (komanda,laikas,data) values ('" + komanda + "','','')");
                 
                 komanda = "T\\\\7\\\\LG" + id + "\\\\A\\\\\\\\";
                 dbc.sqlExecute("insert into komandos (komanda,laikas,data) values ('" + komanda + "','','')");
             }
             else
             {
                 MessageBox.Show("Tik rankiniame režime!");
             }
         }

         public static void stopLG(string id)
         {
             if (!autoTimer)
             {
             DataBase dbc = new DataBase(dbServer, db_user, db_password, db);

             string komanda = "I\\\\1\\\\1\\\\D\\\\A\\\\8\\\\0\\\\0\\\\1\\\\\\\\";
             dbc.sqlExecute("insert into komandos (komanda,laikas,data) values ('" + komanda + "','','')");
                 
           //  komanda = "T\\\\14\\\\A\\\\\\\\";
            // komanda = "T\\\\7\\\\tuscias\\\\A\\\\\\\\";
            // dbc.sqlExecute("insert into komandos (komanda,laikas,data) values ('" + komanda + "','','')");

             komanda = "T\\\\7\\\\LG" + id + "out\\\\A\\\\\\\\";
             dbc.sqlExecute("insert into komandos (komanda,laikas,data) values ('" + komanda + "','','')"); 
             }
             else
             {
                 MessageBox.Show("Tik rankiniame režime!");
             }
         }

         public static void getPlayList(TreeView tv,TreeView tv1)
         {
             TreeViewItem reportazai = new TreeViewItem() { Header = "Reportažai" };

             DataBase dbc = new DataBase(dbServer, db_user, db_password, db);
             System.Data.DataTable tb = dbc.getTable("select reports.name as rpname, reports.id as rpid from reports order by rpname asc");
             if (tb.Rows.Count > 0)
             {
                 tv.Items.Clear();
                 tv1.Items.Clear();
                 for (int i = 0; i < tb.Rows.Count; i++)
                 {

                    string reportstx = "";
                     if (!tb.Rows[i]["rpname"].Equals(null))
                     {
                         reportstx = tb.Rows[i]["rpname"].ToString();
                     }
               
                     string reportsid = "";
                     if (!tb.Rows[i]["rpid"].Equals(null))
                     {
                         reportsid = tb.Rows[i]["rpid"].ToString();
                     }

                     TreeViewItem reports = new TreeViewItem() { Header = reportstx, Tag = ("0_" + reportsid) };
                  
                     bool exists_rep_item = false;
                     int rep_item_ix = -1;
                     for (int y = 0; y < reportazai.Items.Count; y++)
                     {
                         if (reportazai.Items[y].ToString().Contains(reportstx))
                         {
                             exists_rep_item = true;
                             rep_item_ix = y;
                         }
                     }
                     if (!exists_rep_item)
                     {
                         TreeViewItem reportsr = new TreeViewItem() { Header = reportstx, Tag = ("0_" + reportsid) };
                         reportazai.Items.Add(reportsr);
                     }

                 }
             }
             tv1.Items.Add(reportazai);

            /* TreeViewItem playlist = new TreeViewItem() { Header = "PlayList'as" };
             TreeViewItem reportazai = new TreeViewItem() { Header = "Reportažai" };
            
             DataBase dbc = new DataBase(dbServer, db_user, db_password, db);
             System.Data.DataTable tb = dbc.getTable("select news.name as nwname,reports.name as rpname, news.id as nwid, reports.id as rpid from news left join news_reps on news.id=news_reps.news_id left join reports on news_reps.report_id=reports.id");
             if (tb.Rows.Count > 0)
             {
                 tv.Items.Clear();
                 tv1.Items.Clear();
                 for (int i = 0; i < tb.Rows.Count; i++)
                 {

                         string newstx = "";
                         if (!tb.Rows[i]["nwname"].Equals(null))
                         {
                             newstx=tb.Rows[i]["nwname"].ToString();
                         }
                         string reportstx = "";
                         if (!tb.Rows[i]["rpname"].Equals(null))
                         {
                             reportstx=tb.Rows[i]["rpname"].ToString();
                         }
                         string newsid = "";
                         if (!tb.Rows[i]["nwid"].Equals(null))
                         {
                             newsid = tb.Rows[i]["nwid"].ToString();
                         }
                         string reportsid = "";
                         if (!tb.Rows[i]["rpid"].Equals(null))
                         {
                             reportsid = tb.Rows[i]["rpid"].ToString();
                         }

                         TreeViewItem news = new TreeViewItem() { Header = newstx, Tag = newsid };
                         TreeViewItem reports = new TreeViewItem() { Header = reportstx, Tag = (newsid+"_"+reportsid) };
                         bool exists_pl_item = false;
                         int pl_item_ix = -1;
                         for (int y = 0; y < playlist.Items.Count;y++ )
                         {
                             if (playlist.Items[y].ToString().Contains(newstx))
                             {
                                 exists_pl_item = true;
                                 pl_item_ix = y;
                             }
                         }
                         if (exists_pl_item)
                         {
                             TreeViewItem news_item = (TreeViewItem)playlist.Items.GetItemAt(pl_item_ix);
                             if (!reportstx.Equals(""))
                             {
                                 news_item.Items.Add(reports);
                             }
                         }
                         else
                         {
                             if (!reportstx.Equals(""))
                             {
                                 news.Items.Add(reports);
                             }
                             playlist.Items.Add(news);
                         }

                         bool exists_rep_item = false;
                         int rep_item_ix = -1;
                         for (int y = 0; y < reportazai.Items.Count; y++)
                         {
                             if (reportazai.Items[y].ToString().Contains(reportstx))
                             {
                                 exists_rep_item = true;
                                 pl_item_ix = y;
                             }
                         }
                         if (!exists_rep_item)
                         {
                             TreeViewItem reportsr = new TreeViewItem() { Header = reportstx, Tag = (newsid + "_" + reportsid) };
                             reportazai.Items.Add(reportsr);
                         }

                 }
             }
             tv.Items.Add(playlist);
             tv1.Items.Add(reportazai);*/
         }

         public static ObservableCollection<Anonsai> getAnonsai(string report_id)
         {
             ObservableCollection<Anonsai> an = new ObservableCollection<Anonsai>();
             DataBase dbc = new DataBase(dbServer, db_user, db_password, db);
             string sql = "select * from anonsai order by id asc";
             if (!report_id.Equals(""))
             {
                 sql = "select * from anonsai where report_id=0 order by id asc";// +report_id;
             }
             System.Data.DataTable tbt = dbc.getTable(sql);
             for (int i = 0; i < tbt.Rows.Count; i++)
             {
                 int id = int.Parse(tbt.Rows[i]["id"].ToString());
                 string name = tbt.Rows[i]["name"].ToString();
                 string template = tbt.Rows[i]["template"].ToString();
                 string txt_template = tbt.Rows[i]["txt_template"].ToString();
                 //string tekstas = tbt.Rows[i]["tekstas"].ToString();
                 an.Add(new Anonsai() { nr = id, Title = name });
                 string komanda = "";
                 komanda = "W\\\\AN" + id + "\\\\" + template + "\\\\\\\\";
                 dbc.sqlExecute("insert into komandos (komanda,laikas,data) values ('" + komanda + "','','')");
                 System.Data.DataTable tbant = dbc.getTable("Select * from anonsai_eteris");
                 for (int y = 0; y < tbant.Rows.Count; y++)
                 {
                     string antekstas = tbant.Rows[y]["tekstas"].ToString();
                     komanda = "W\\\\ANT" + id + "_"+(y+1).ToString()+"\\\\" + txt_template + "\\\\" + antekstas + "\\\\\\\\";
                     dbc.sqlExecute("insert into komandos (komanda,laikas,data) values ('" + komanda + "','','')");
                 }
             }
             return an;
         }

         public static void generateAnonsaiTemplates(string report_id)
         {
             DataBase dbc = new DataBase(dbServer, db_user, db_password, db);
             string sql = "select * from anonsai";
             if (!report_id.Equals(""))
             {
                 sql = "select * from anonsai where report_id=" + report_id + " order by id asc";
             }
             System.Data.DataTable tbt = dbc.getTable(sql);
             for (int i = 0; i < tbt.Rows.Count; i++)
             {
                 int id = int.Parse(tbt.Rows[i]["id"].ToString());
                 string name = tbt.Rows[i]["name"].ToString().Replace("'", "''");
                 string template = tbt.Rows[i]["template"].ToString();
                 string txt_template = tbt.Rows[i]["txt_template"].ToString();
                 //string tekstas = tbt.Rows[i]["tekstas"].ToString();
                 string komanda = "";
                 komanda = "W\\\\AN" + id + "\\\\" + template + "\\\\\\\\";
                 dbc.sqlExecute("insert into komandos (komanda,laikas,data) values ('" + komanda + "','','')");
                 System.Data.DataTable tbant = dbc.getTable("Select * from anonsai_tekstai where anonso_id=" + id);
                 for (int y = 0; y < tbant.Rows.Count; y++)
                 {
                     string antekstas = tbant.Rows[y]["tekstas"].ToString().Replace("'", "''");
                     komanda = "W\\\\ANT" + id + "_" + (y + 1).ToString() + "\\\\" + txt_template + "\\\\" + antekstas + "\\\\\\\\";
                     dbc.sqlExecute("insert into komandos (komanda,laikas,data) values ('" + komanda + "','','')");
                 }
             }
         }

         public static void startAN(string id)
         {
             DataBase dbc = new DataBase(dbServer, db_user, db_password, db);
             string komanda = "I\\\\1\\\\2\\\\D\\\\A\\\\8\\\\0\\\\0\\\\1\\\\\\\\";
             dbc.sqlExecute("insert into komandos (komanda,laikas,data) values ('" + komanda + "','','')");
             komanda = "T\\\\7\\\\AN" + id + "\\\\B\\\\\\\\";
             dbc.sqlExecute("insert into komandos (komanda,laikas,data) values ('" + komanda + "','','')");
             komanda = "I\\\\1\\\\1\\\\D\\\\A\\\\8\\\\0\\\\0\\\\1\\\\\\\\";
             dbc.sqlExecute("insert into komandos (komanda,laikas,data) values ('" + komanda + "','','')");
             komanda = "T\\\\7\\\\ANT" + id + "_1\\\\A\\\\\\\\";
             dbc.sqlExecute("insert into komandos (komanda,laikas,data) values ('" + komanda + "','','')");
         }

         public static void kitasAN(string id,string ix)
         {
             DataBase dbc = new DataBase(dbServer, db_user, db_password, db);
             string komanda = "I\\\\1\\\\1\\\\D\\\\A\\\\8\\\\0\\\\0\\\\1\\\\\\\\";
             dbc.sqlExecute("insert into komandos (komanda,laikas,data) values ('" + komanda + "','','')");
             komanda = "T\\\\7\\\\ANT" + id + "_" + ix + "\\\\A\\\\\\\\";
             dbc.sqlExecute("insert into komandos (komanda,laikas,data) values ('" + komanda + "','','')");
         }

         public static void stopAN()
         {
             DataBase dbc = new DataBase(dbServer, db_user, db_password, db);
             string komanda = "I\\\\1\\\\1\\\\D\\\\A\\\\8\\\\0\\\\0\\\\1\\\\\\\\";
             dbc.sqlExecute("insert into komandos (komanda,laikas,data) values ('" + komanda + "','','')");
             komanda = "I\\\\1\\\\2\\\\D\\\\A\\\\8\\\\0\\\\0\\\\1\\\\\\\\";
             dbc.sqlExecute("insert into komandos (komanda,laikas,data) values ('" + komanda + "','','')");
             komanda = "T\\\\14\\\\A\\\\\\\\";
             komanda = "T\\\\7\\\\tuscias\\\\A\\\\\\\\";
             dbc.sqlExecute("insert into komandos (komanda,laikas,data) values ('" + komanda + "','','')");
             komanda = "T\\\\14\\\\B\\\\\\\\";
             komanda = "T\\\\7\\\\tuscias\\\\B\\\\\\\\";
             dbc.sqlExecute("insert into komandos (komanda,laikas,data) values ('" + komanda + "','','')");
         }

         public static void getVedejai(ComboBox cb)
         {
             try
             {
                 DataBase dbc = new DataBase(dbServer, db_user, db_password, db);
                 string sql = "select * from vedejai";
                 System.Data.DataTable tb = dbc.getTable(sql);
                 try
                 {
                     cb.Items.Clear();
                 }
                 catch (Exception ex) { }
                 for (int i = 0; i < tb.Rows.Count; i++)
                 {
                     //string template=tb.Rows[i]["template"].ToString();
                     //int id = int.Parse(tb.Rows[i]["id"].ToString());
                     string name = tb.Rows[i]["name"].ToString();
                     cb.Items.Add(name);//new Vedejai() { nr = id, Title = name });
                     //string komanda = "W\\VT\\vedejo_titras\\"+name+"\\\\";
                     // dbc.sqlExecute("insert into komandos (komanda,laikas,data) values ('" + komanda + "','','')");
                 }
                 cb.SelectedIndex = cb.Items.IndexOf(tb.Rows[0]["name"].ToString());
             }
             catch (Exception ex) { AddToEventLog("InscriberControlClient", "getVedejai skaitymo klaida: " + ex.Message, true); }

         }

         public static void getVedejaiList(ListBox ls)
         {
             try
             {
                 DataBase dbc = new DataBase(dbServer, db_user, db_password, db);
                 string sql = "select * from vedejai";
                 System.Data.DataTable tb = dbc.getTable(sql);
                 ls.Items.Clear();
                 for (int i = 0; i < tb.Rows.Count; i++)
                 {
                     int id = int.Parse(tb.Rows[i]["id"].ToString());
                     string name = tb.Rows[i]["name"].ToString();
                     ls.Items.Add(name);//new Vedejai() { nr = id, Title = name });
                 }
                 ls.SelectedIndex = ls.Items.IndexOf(tb.Rows[0]["name"].ToString());
             }
             catch (Exception ex) { AddToEventLog("InscriberControlClient", "getVedejaiList skaitymo klaida: " + ex.Message, true); }

         }

         public static void createVT(ComboBox cb)
         {
             DataBase dbc = new DataBase(dbServer, db_user, db_password, db);
             string text=cb.SelectedValue.ToString();
             MessageBox.Show(text);
             string komanda = "W\\\\VT\\\\vedejo_titras\\\\" + text + "\\\\\\\\";
             dbc.sqlExecute("insert into komandos (komanda,laikas,data) values ('" + komanda + "','','')");
         }

         public static void CleanChanel(string ch)
         {
             DataBase dbc = new DataBase(dbServer, db_user, db_password, db);
             string komanda = "T\\\\14\\\\"+ch+"\\\\\\\\";
             komanda = "T\\\\7\\\\tuscias\\\\"+ch+"\\\\\\\\";
             dbc.sqlExecute("insert into komandos (komanda,laikas,data) values ('" + komanda + "','','')");
             
         }

         internal static void StartFonas(CheckBox checkBox7)
         {
             DataBase dbc = new DataBase(dbServer, db_user, db_password, db);
             string komanda = "";
             komanda = "I\\\\1\\\\3\\\\D\\\\A\\\\8\\\\0\\\\0\\\\1\\\\\\\\";
             dbc.sqlExecute("insert into komandos (komanda,laikas,data) values ('" + komanda + "','','')");
                 
             if (checkBox7.IsChecked==true)
             {
                 komanda = "T\\\\7\\\\juosta_paprasta_K\\\\C\\\\\\\\";
             }
             else
             {
                 komanda = "T\\\\7\\\\juosta_paprasta\\\\C\\\\\\\\";
             }
             dbc.sqlExecute("insert into komandos (komanda,laikas,data) values ('" + komanda + "','','')");
         }

         public static void getAnonsaiParts(ListBox ls)
         {
             ListBox lsEteris = new ListBox();
             DataBase dbc = new DataBase(dbServer, db_user, db_password, db);
             System.Data.DataTable tbe = dbc.getTable("select * from anonsai_eteris where id>0 order by id asc");
             if (tbe.Rows.Count > 0)
             {
                 for (int i = 0; i < tbe.Rows.Count; i++)
                 {
                     int id = int.Parse(tbe.Rows[i]["id"].ToString());
                     string tekstas = tbe.Rows[i]["tekstas"].ToString();
                     lsEteris.Items.Add(new AnonsaiPartsEtersis() { id = id, tekstas = tekstas });
                 }
             }
             System.Data.DataTable tb = dbc.getTable("select * from anonsai_tekstai where anonso_id=1 order by id asc");
             if (tb.Rows.Count > 0)
             {
                 for (int i = 0; i < tb.Rows.Count; i++)
                 {
                     int id = int.Parse(tb.Rows[i]["id"].ToString());
                     int ans_id = int.Parse(tb.Rows[i]["anonso_id"].ToString());
                     string tekstas = tb.Rows[i]["tekstas"].ToString();
                     bool etr = isInEterisList(lsEteris,tekstas);
                     ls.Items.Add(new AnonsaiParts() { id = id, anonso_id = 1, tekstas = tekstas, eteris=etr });
                 }
             }
         }

         public static bool isInEterisList(ListBox ls, string tekstas)
         {
             bool rez = false;
             for (int i=0;i<ls.Items.Count;i++)
             {
                 AnonsaiPartsEtersis ap = (AnonsaiPartsEtersis)ls.Items[i];
                 if (ap.tekstas.Equals(tekstas)){
                     return true;
                 }
             }
             return rez;
         }

         internal static void AddAnonsaoEtersi(ListBox listBox1)
         {
             DataBase dbc = new DataBase(dbServer, db_user, db_password, db);
             dbc.sqlExecute("delete from anonsai_eteris where id>0");
             for (int i = 0; i < listBox1.Items.Count; i++)
                 {
                    AnonsaiParts ap=(AnonsaiParts)listBox1.Items[i];
                    if (ap.eteris)
                    {
                        dbc.sqlExecute("insert into anonsai_eteris (tekstas) values ('"+ap.tekstas+"')");
                    }
                 }
             dbc.sqlExecute("delete from anonsai_tekstai where id>0");
             for (int i = 0; i < listBox1.Items.Count; i++)
             {
                 AnonsaiParts ap = (AnonsaiParts)listBox1.Items[i];
                 dbc.sqlExecute("insert into anonsai_tekstai (anonso_id,tekstas) values (1,'" + ap.tekstas + "')");
             }
         }

         public static void setChanelMix()
         {
             DataBase dbc = new DataBase(dbServer, db_user, db_password, db);
             //string komanda = "T\\\\7\\\\juosta_paprasta_K\\\\C\\\\\\\\";
             string komanda = "I\\\\1\\\\1\\\\D\\\\A\\\\7\\\\0\\\\0\\\\1\\\\\\\\";
             dbc.sqlExecute("insert into komandos (komanda,laikas,data) values ('" + komanda + "','','')");
             komanda = "I\\\\1\\\\2\\\\D\\\\A\\\\7\\\\0\\\\0\\\\1\\\\\\\\";
             dbc.sqlExecute("insert into komandos (komanda,laikas,data) values ('" + komanda + "','','')");
             komanda = "I\\\\1\\\\3\\\\D\\\\A\\\\7\\\\0\\\\0\\\\1\\\\\\\\";
             dbc.sqlExecute("insert into komandos (komanda,laikas,data) values ('" + komanda + "','','')");
          }

         internal static void removeVedėjasByName(string name)
         {
             try
             {
                 DataBase dbc = new DataBase(dbServer, db_user, db_password, db);
                 string sql = "delete from vedejai where name='" + name + "'" ;
                 dbc.sqlExecute(sql);
             }
             catch (Exception ex) { AddToEventLog("InscriberControlClient", "getVedejaiList skaitymo klaida: " + ex.Message, true); }

         }

         internal static void insertVedėjasByName(string name)
         {
             if (!name.Equals(""))
             {
                 try
                 {
                     DataBase dbc = new DataBase(dbServer, db_user, db_password, db);
                     string sql = "select * from vedejai";
                     System.Data.DataTable tb = dbc.getTable(sql);
                     sql = "insert into vedejai (id,name) values (" + (tb.Rows.Count + 1).ToString() + ",'" + name + "')";
                     dbc.sqlExecute(sql);
                 }
                 catch (Exception ex) { AddToEventLog("InscriberControlClient", "getVedejaiList skaitymo klaida: " + ex.Message, true); }
             }
         }

         internal static void DragMoveNew(Window window, System.Windows.Input.MouseEventArgs e, System.Windows.Point pt)
         {
             if ((pt.Y - lastPosition.Y > 20) || (pt.Y - lastPosition.Y < 20))
             {
                 window.Top = window.Top + pt.Y - lastPosition.Y;
             }
             if ((pt.X- lastPosition.X> 20) || (pt.X - lastPosition.X < 20))
             {
                 window.Left = window.Left + pt.X - lastPosition.X;
                 //lastPosition = pt;
             }
             lastPosition = pt;
         }
    
   }
}
